﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IterationLife.Domains.Sports.Dtos.SportReports;
using IterationLife.Domains.Sports.Repositories;
using Util.Domains;
using Util.Exceptions;

namespace IterationLife.Domains.Sports.Services
{
    public class SportDataManager : DomainServiceBase, ISportDataManager
    {
        private ISportEventRepository SportEventRepository { get; set; }
        private ISportEventGroupPersonRepository SportEventGroupPersonRepository { get; set; }
        private ISportUserRepository SportUserRepository { get; set; }
        /// <summary>
        /// 构造
        /// </summary>
        public SportDataManager(ISportUserRepository sportUserRepository, ISportEventRepository sportEventRepository, ISportEventGroupPersonRepository sportEventGroupPersonRepository)
        {
            SportEventRepository = sportEventRepository;
            SportEventGroupPersonRepository = sportEventGroupPersonRepository;
            SportUserRepository = sportUserRepository;
        }


        /// <summary>
        /// 获取用户的锻炼时间线
        /// </summary>
        public List<SportTimeLineData> GetTimeLineDatas(string uid)
        {
            var mtUser = SportUserRepository.Find(t => t.PartnerUserId == uid).FirstOrDefault();
            if (mtUser == null)
                throw new Warning("绑定用户不存在");
            var eventPersonDatas = SportEventGroupPersonRepository.Find(t => t.SportEventGroup.SportEvent.StartTime != null && t.SportAthleteId == mtUser.SportAthlete.Id).OrderBy(t => t.SportEventGroup.SportEvent.StartTime).ToList();
            var result = new List<SportTimeLineData>();
            foreach (var personData in eventPersonDatas)
            {
                result.Add(new SportTimeLineData(personData, personData.GetJsonData()));
            }
            return result;
        }
    }
}
